﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.UI;
using System.IO;
using System.Web.WebPages;

namespace DNA.Mvc.jQuery
{
    public class RichTextBoxBuilder : jQueryComponentBuilder<RichTextBoxOptions, RichTextBox, RichTextBoxBuilder>
    {
        public RichTextBoxBuilder(RichTextBox rte, ViewContext viewContext, IViewDataContainer viewContainer) : base(rte, viewContext, viewContainer)
        {
            ToolPanes(panes =>
            {
                panes.Add().AddFontTools();
                panes.Add().AddParagraphTools().AddFormatTools().AddClipboardTools();
            });
        }
        private bool resizable = false;

        protected override string jQueryPluginName
        {
            get { return "richtextbox"; }
        }

        public RichTextBoxBuilder Value(Func<object, object> value)
        {
            return this.Value(value(null).ToString());
        }

        //public RichTextBoxBuilder Value(Action value)
        //{
        //    var _val = new StringBuilder();
        //    var writer = new StringWriter(_val);
        //    value.
        //    //Component.BeforeContentRender += new ViewComponentRenderDelegate((writer) =>
        //    //{
        //    //    value.Invoke();
        //    //});
        //    return this;
        //}

        public RichTextBoxBuilder Value(string value)
        {
            Component.Value = value;
            //Component.BeforeComponentRender+=new ViewComponentRenderDelegate((writer)=>{
            //    writer.Write(value);
            //});
            return this;
        }

        public RichTextBoxBuilder ToolPanes(Action<RichTextBoxPaneFactory> panes)
        {
            var factory = new RichTextBoxPaneFactory(this.Component, this.ViewContext,this.ViewDataContainer);
            panes.Invoke(factory);
            return this;
        }

        public RichTextBoxBuilder Resizable()
        {
            resizable = true;
            return this;
        }

        public RichTextBoxBuilder AllowHTMLEditing(bool value)
        {
            Component.AllowEditSource = value;
            return this;
        }

        public override void Render()
        {
            _PreRender();

   
            base.Render();
            _InitStartup();
        }

        public override MvcHtmlString GetHtml()
        {
            _PreRender();

            //if (Component.AllowEditSource)
            //{
            //    Component.StatusBar = new Func<object,object>(obj =>
            //    {
            //        return new HelperResult(writer =>
            //        {
            //           writer.Write( _InitToggleButton().GetHtml());
            //        });
            //    });
            //}

            var _html=base.GetHtml();
            _InitStartup();
            return _html;
        }

        private void _PreRender()
        {
            if (Component.OnNormalized.Length > 0)
                this.Options(opts =>
                {
                    opts.OnDOMNodeNormalized += Component.OnNormalized;
                });

            if (Component.AllowEditSource)
            {
                Component.StatusBar = new Action(() =>
                {
                    _InitToggleButton().Render();
                });
            }
        }

        private ButtonBuilder _InitToggleButton()
        {
            return Helper.Dna().Button(Component.Id + "_toggleButton")
                                    .Click(jQuerySelector + ".richtextbox(\"toggleview\");")
                                    .Icons("d-rte-icon d-rte-design")
                                    .States(states =>
                                    {
                                        states.Add("Design")
                                            .Icons("d-rte-icon d-rte-design");
                                        states.Add("HTML").Select(true)
                                            .Icons("d-rte-icon d-rte-html");
                                    });
        }

        private void _InitStartup()
        {
            if (resizable)
                Helper.Resizable("#" + Component.Id + "_container", new ResizableOptions()
                {
                    AlsoResize = "#" + Component.Id + "_container .d-rte-editor",
                    OnResize = this.jQuerySelector + ".richtextbox(\"resize\");"
                });
            Helper.Sortable("#" + Component.Id + "_panes>li", new SortableOptions()
            {
                DragHandler = ".d-toolbar-draghandler",
                ConnectWith = "#" + Component.Id + "_panes>li",
                DragHelper = Helpers.Clone,
                ForceHelperSize = true,
                ForcePlaceholderSize = true,
                PlaceHolderCssClass = "ui-widget-placeholder"
            });

            Helper.Droppable("#" + Component.Id + "_panes>li", new DroppableOptions()
            {
                Accept = ".d-toolbar",
                ActiveCssClass = "ui-state-default",
                HoverCssClass = "ui-state-highlight"
            });
        }
    }
}
